<?php
include '../admin_head.php';
//include '../common.php';

//处理搜索分页数据
	$name = $_GET['name'];
	$sex = $_GET['sex'];
	$class = $_GET['class'];
	
	if($name != null) $search[] = "name like '%{$name}%'";
	if($sex != null) $search[] = "sex='{$sex}'";
	if($class != null) $search[] = "class='{$class}'";
	
	if($search != null ) $where = 'where '. implode(' and ',$search);
	//重点理解三种情况的where的取值
	$where = empty($_GET['where']) ? $where : stripslashes($_GET['where']);

//获取总记录数
    $sql = "select count(id) num from ". PRE ."user {$where}" ;
    
    $result = mysql_query($sql);
    
    if($result && mysql_affected_rows() > 0){
        $row = mysql_fetch_assoc($result);
    }

    $total = $row['num'];       //总记录数
    //每页显示数
    $num = 10;

    //总页数
    $amount = ceil($total / $num);

    //页码
    $page = empty($_GET['p'])? 1 : (int)$_GET['p'];

    if($page < 1){
        $page = 1;
    }

    if($page > $amount){
        $page = $amount;
    }

    //偏移量
    $offset = ($page - 1) * $num;  



$sql = "select id,name,sex,email,tel,reg_time,jifen,class,disable from ".PRE."user {$where} order by id limit $offset,$num";
$result = mysql_query($sql);
if($result && mysql_num_rows($result) > 0){
	while($row = mysql_fetch_assoc($result)){
		$userlist[] = $row;
	}
}
//制作分页按钮
 $start = ($page - 3) <= 1 ? 1 : $page - 3;

    $end = ($page + 3) >= $amount ? $amount : $page + 3 ;

    for($i = $start; $i <= $end; $i++){
        if($i == $page){
            $str .= '<li><a style="color:#E50065;font-weight:bold;" href="index.php?where='.$where.'&p='. $i .'" >['. $i .']</a></li>';
            continue;
        }
            $str .= '<li><a href="index.php?where='.$where.'&p='. $i .'">['. $i .']</a><li>';

    }


    $nav = <<<tag
        <li><a href="index.php?where={$where}&p=1">首页</a></li>
        $str
        <li><a href="index.php?where={$where}&p=$amount">尾页</a></li>
tag;



?>

	<div class="admin_main_nav">用户管理&gt;&gt;用户列表</div>
	<div id="user_index" class="admin_main_index">
		<div class="width">
			<div class="user_index_title">
				<div class="user_search fr">
					<form action="index.php" method="get">
						用户名：<input type="text" name="name"/>
						性别：
							<select name="sex">
								<option value="">全部</option>
								<option value="0">女</option>
								<option value="1">男</option>
								<option value="2">保密</option>
							</select>
						用户权限：
							<select name="class">
								<option value="">全部</option>
								<option value="0">超级管理员</option>
								<option value="1">管理员</option>
								<option value="2">普通会员</option>
							</select>
							<input type="submit" value="搜索" />
					</form>
				</div>	
			</div>
			<div>
				<table width="800" align="center">
					<tr>
						<th>用户名</th>
						<th>性别</th>
						<th>邮箱</th>
						<th>注册时间</th>
						<th>用户级别</th>
						<th>积分</th>
						<th>启用</th>
						<th width="260">操作</th>
					</tr>
					<?php if(!empty($userlist)):?>
					<?php foreach($userlist as $tr):?>
					<tr>
						<td><?php echo $tr['name'] ?></td>
						<td>
							<?php 
								if($tr['sex'] == 0) echo '女'; 
								if($tr['sex'] == 1) echo '男'; 
								if($tr['sex'] == 2) echo '保密'; 
							?>
						</td>
						<td><?php echo $tr['email'] ?></td>
						<td><?php echo date('Y-m-d',$tr['reg_time']) ?></td>
						<td>
							<?php 
								if($tr['class']  == 0) echo '超级管理员';
								if($tr['class']  == 1) echo '管理员';
								if($tr['class']  == 2) echo '普通会员';
							?>
						</td>
						<td><?php echo $tr['jifen'] ?></td>
						<td><?php 
							if($tr['disable'] == 0) echo '<a href="action.php?a=dis&value=1&id='.$tr['id'].'"><img src="../../images/yes.gif" /></a>';
							if($tr['disable'] == 1) echo '<a href="action.php?a=dis&value=0&id='.$tr['id'].'"><img src="../../images/no.gif" /></a>';
 
						?></td>
						<td>
							<div class="action">
								<div class="fl"><a class="edit" href="edit.php?id=<?php echo $tr[id] ?>"><div class="icon fl edit_i"></div><div class="fl">编辑</div></a></div>
								<div class="fl"><a class="del" href="action.php?a=del&id=<?php echo $tr[id] ?>"><div class="icon fl del_i"></div><div class="fl">删除</div></a></div>
								<div class="fl"><a class="reset" href="action.php?a=resetpwd&id=<?php echo $tr[id] ?>"><div class="icon fl reset_i"></div><div class="fl">重置密码</div></a></div>
							</div>
						</td>
					</tr>
					<?php endforeach;?>
					<?php else:?>
						<tr>
							<td colspan="7">没有找到任何记录</td>
						</tr>
					<?php endif;?>
				</table>
			</div>
			<!-- 分页按钮 -->
			<div class="nav">
				<div class="tips"><?php $start = $offset + 1>0? $offset+1: 0;$end = $offset + $num >= $total? $total : $offset + $num; echo "Showing $start to $end of $total entries";?></div>
				<ul class="user_nav">
					<?php echo $nav?>
				</ul>
			</div>
		</div>
	</div>

	
<?php include '../admin_footer.php'; ?>

